package com.liusha.quicksand.web;

import com.liusha.quicksand.po.Blog;
import com.liusha.quicksand.po.User;
import com.liusha.quicksand.service.BlogService;
import com.liusha.quicksand.service.TypeService;
import com.liusha.quicksand.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 一世长安一世欢
 * @Date: 2020/10/18
 * @Time: 15:12
 * @Description: No Description
 */

@Controller
@RequestMapping("/post")
public class BlogPostController {

    @Autowired
    private TypeService typeService;

    /*  博文发布页 */
    @GetMapping
//    @GetMapping("/post")
    public String types(@PageableDefault(size = 12,sort = {"updatetime"},direction = Sort.Direction.DESC) Pageable pageable, BlogQuery blog, Model model) {

        model.addAttribute("type",typeService.listType());

        model.addAttribute("page",blogService.listBlog(pageable,blog));
        return "post";
    }

//    private static final String REDIRECT_LIST = "redirect:/admin/{list}/{article-management}";
    private static final String REDIRECT_LIST = "redirect:/index";

    @Autowired
    private BlogService blogService;

    private void setTypes(Model model){
        model.addAttribute("types",typeService.listType());
    }

    @PostMapping("/admin/{list}/{article-management}")
    public String post(Blog blog, RedirectAttributes attributes, HttpSession session) {

        blog.setUser((User) session.getAttribute("user"));

        blog.setType(typeService.getType(blog.getType().getId()));

        Blog b;

        if (blog.getBlogId() == null) {
            b = blogService.saveBlog(blog);
        } else {
            b = blogService.updateBlog(blog.getBlogId(), blog);
        }


        if (b == null) {
            attributes.addFlashAttribute("message", "发布失败");
        } else {
            attributes.addFlashAttribute("message", "发布成功");
        }

        return REDIRECT_LIST;

    }

/*新增页面*/
//    @GetMapping("/input")
//    public String input(Model model) {
//
//        model.addAttribute("blog", new Blog());
//
//        return "post";
//    }

    /*修改操作*/
//    @GetMapping({"/{list}/{article-management}/{blogId}/input"})
//    public String editInput(@PathVariable Long blogId, Model model) {
//
//        Blog blog = blogService.getBlog(blogId);
//
////        blog.init();
//
//        model.addAttribute("blog", blog);
//
//        return "admin/p-post";
//    }
}
